package com.huluobo.a2202mvvm_im_douyindemo.contact;

import android.util.Log;
import android.view.View;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.huluobo.a2202mvvm_im_douyindemo.R;
import com.huluobo.a2202mvvm_im_douyindemo.adapter.ContactAdapter;
import com.huluobo.a2202mvvm_im_douyindemo.databinding.FragmentContactBinding;
import com.huluobo.a2202mvvm_im_douyindemo.mvvmBase.BaseMVVMFragment;
import com.huluobo.a2202mvvm_im_douyindemo.viewDemo.SlideBar;
import com.hyphenate.EMContactListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.modules.contact.EaseContactListFragment;

import java.util.ArrayList;
import java.util.List;

public class ContactFragment extends BaseMVVMFragment<FragmentContactBinding, ContactViewModel> {

    private ContactAdapter contactAdapter;
    private List<ContactListItemBean> contactListItemBeanList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_contact;
    }

    @Override
    protected void initData() {
        viewModel.getAllContacts();

        viewModel.getAllContactsSuccess.observe(this, contactListItemBeans -> {
            dataBinding.contactSrl.setRefreshing(false);
            contactListItemBeanList.clear();
            contactListItemBeanList.addAll(contactListItemBeans);
            contactAdapter = new ContactAdapter(contactListItemBeans);
            dataBinding.contactRv.setAdapter(contactAdapter);
        });

        viewModel.getAllContactsFailed.observe(this, s -> {
            dataBinding.contactSrl.setRefreshing(false);
            Log.i("TagA", "contacts:" + s);
        });
    }

    @Override
    protected void initView() {
        dataBinding.contactRv.setLayoutManager(new LinearLayoutManager(requireContext()));
        contactAdapter = new ContactAdapter(contactListItemBeanList);
        dataBinding.contactRv.setAdapter(contactAdapter);

        EMClient.getInstance().contactManager().setContactListener(new EMContactListener() {
            @Override
            public void onContactAdded(String username) {

            }

            @Override
            public void onContactDeleted(String username) {
                viewModel.getAllContacts();
            }

            @Override
            public void onContactInvited(String username, String reason) {

            }

            @Override
            public void onFriendRequestAccepted(String username) {

            }

            @Override
            public void onFriendRequestDeclined(String username) {

            }
        });

        dataBinding.contactSearchBtn.setOnClickListener(v -> {
            searchContact(dataBinding.contactSearchEt.getText().toString());
        });

        dataBinding.contactSrl.setOnRefreshListener(() -> {
            viewModel.getAllContacts();
        });

        dataBinding.contactSlideBar.setOnSectionChangeListener(new SlideBar.OnSectionChangeListener() {
            @Override
            public void onSectionChange(String firstLetter) {
                dataBinding.contactSectionTv.setVisibility(View.VISIBLE);
                dataBinding.contactSectionTv.setText(firstLetter);
                moveToLetterPosition(firstLetter);
            }

            @Override
            public void onSlideFinish() {
                dataBinding.contactSectionTv.setVisibility(View.GONE);
            }
        });
    }

    private void moveToLetterPosition(String firstLetter) {
        for (int i = 0; i < contactListItemBeanList.size(); i++) {
            if (String.valueOf(contactListItemBeanList.get(i).getFirstLetter()).equals(firstLetter)) {
//                dataBinding.contactRv.scrollToPosition(i);
                dataBinding.contactRv.smoothScrollToPosition(i);
            }
        }
    }

    private void searchContact(String username) {
        if (username.isEmpty()) {
            viewModel.getAllContacts();
            return;
        }

        List<ContactListItemBean> newList = new ArrayList<>();
        for (int i = 0; i < contactListItemBeanList.size(); i++) {
            if (username.equals(contactListItemBeanList.get(i).getUsername())) {
                ContactListItemBean contactListItemBean = new ContactListItemBean(
                        username, username.charAt(0), true
                );
                newList.add(contactListItemBean);
            }
        }

        contactAdapter = new ContactAdapter(newList);
        dataBinding.contactRv.setAdapter(contactAdapter);
    }

    @Override
    protected Class<ContactViewModel> getViewModel() {
        return ContactViewModel.class;
    }
}